/**
 * 
 */
package br.ufrgs.inf.cwsmart.impl.matcher;

import java.util.List;

import org.uc3m.verbus.bpel.model.Activity;
import org.uc3m.verbus.bpel.model.PartnerLinks;
import org.uc3m.verbus.bpel.model.Receive;
import org.uc3m.verbus.bpel.model.Sequence;
import org.uc3m.verbus.bpel.parser.Parser;
import org.uc3m.verbus.bpel.util.BPWSUtil;

import br.ufrgs.inf.cwsmart.matcher.CWSProperties;

/**
 * @author Rodrigo S Lumertz
 *
 */
public class BPELProperties extends CWSProperties {	
	
	/**
	 * Parses the cws file to get the informations 
	 * 
	 * @param cwsPath
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void parse(String cwsPath) throws Exception {
						
		Parser parser = null;

		org.uc3m.verbus.util.Logger.logger.setLogLevel(org.uc3m.verbus.util.Logger.MIN_DEBUG_LEVEL);               
           
    	parser = new Parser(cwsPath);
    	        
    	System.out.println("cws name: " + parser.getProcess().getName().toString());    	
    	
    	//getting the number of partnerlink
    	PartnerLinks partnerLinks = parser.getProcess().getPartnerLinks(); 
    	if(partnerLinks != null) {
    		System.out.println("partner_link_number: " + partnerLinks.getPartnerLinks().size());
    		this.mapAttribute.put("partner_link_number", new Integer(partnerLinks.getPartnerLinks().size()).toString());
    	}
    	
    	
    	Activity activity = parser.getProcess().getActivity();
    	
    	//getting the execution type
    	if(activity != null && BPWSUtil.getActivityType(activity) == BPWSUtil.ACT_SEQUENCE) {
    		System.out.println("cws_execution_type: proactive");
    		this.mapAttribute.put("cws_execution_type", "proactive");        		
    	}
    	
    	//getting the cws functionality  
    	if(activity != null && BPWSUtil.getActivityType(activity) == BPWSUtil.ACT_SEQUENCE) {
    		Sequence sequence = (Sequence) activity;
        	List<Activity> lstAct = (List<Activity>) sequence.getActivities();
        	for (Activity act : lstAct) {
				if (BPWSUtil.getActivityType(act) == BPWSUtil.ACT_RECEIVE) {
					String cwsRole = ((Receive) act).getPartnerLink().getMyRole();
					System.out.println("cws functionality: " + cwsRole);
					this.lstFunctionality.add(cwsRole);
				}
			}        	
    	}
    	
	}
}

